//
//  BPDateFormatter.m
//  BPKITsDemo
//
//  Created by LiHaozhen on 14/12/15.
//  Copyright (c) 2014年 ihojin. All rights reserved.
//

#import "BPDateFormatter.h"

@implementation BPDateRange

- (instancetype)initWithStartDate:(NSDate *)start endDate:(NSDate *)end
{
    if (self = [super init]) {
        self.startDate = start;
        self.endDate = end;
    }
    return self;
}

- (BPDateCompareResult)compareDate:(NSDate *)date
{
    if ([self.startDate compare:date] == NSOrderedAscending &&
        [self.endDate compare:date] == NSOrderedAscending) {
        
        return BPDateCompareResultBefore;
    }else if ([self.startDate compare:date] == NSOrderedAscending &&
              [date compare:self.endDate] == NSOrderedAscending){
        
        return BPDateCompareResultValid;
    }else{
        
        return BPDateCompareResultAfter;
    }
}
@end

@implementation BPDateFormatter{
    
    NSMutableDictionary *_fmtters;
}

+ (instancetype)sharedFormatter
{
    static BPDateFormatter *fmtter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        fmtter = [[BPDateFormatter alloc] init];
    });
    return fmtter;
}

- (NSDateFormatter *)fmtterForStyle:(BPDateFormatterStyle)style
{
    NSString *fmt = nil;
    switch (style) {
        case BPDateFormatterStyleDefault:
            fmt = @"yyyy-MM-dd HH:mm:ss";
            break;
        case BPDateFormatterStyle_2:
            fmt = @"yyyy-MM-dd";
            break;
        case BPDateFormatterStyle_3:
            fmt = @"yyyy年MM月dd日";
            break;
        case BPDateFormatterStyle_4:
            fmt = @"yyyyMMdd";
            break;
        case BPDateFormatterStyle_5:
            fmt = @"yyyyMMddHHmmss";
            break;
        case BPDateFormatterStyle_6:
            fmt = @"HH:mm";
            break;
        case BPDateFormatterStyle_7:
            fmt = @"HH:mm:ss";
            break;
        case BPDateFormatterStyle_8:
            fmt = @"mm:ss";
            break;
        case BPDateFormatterStyle_9:
            fmt = @"ss";
            break;
        case BPDateFormatterStyle_10:
            fmt = @"yyyy.MM.dd";
            break;
        case BPDateFormatterStyle_11:
            fmt = @"MM月dd日";
            break;
        case BPDateFormatterStyle_12:
            fmt = @"yyyyMMdd_HHmmss";
            break;
        case  BPDateFormatterStyle_13:
            fmt = @"HHmmss";
            break;
        case BPDateFormatterStyle_14:
            fmt = @"yyyy.MM.dd HH:mm:ss";
            break;
        case BPDateFormatterStyle_15:
            fmt = @"MM-dd";
            break;
        case BPDateFormatterStyle_16:
            fmt = @"yyyy年MM月";
            break;
        case BPDateFormatterStyle_17:
            fmt = @"yyyy/MM/dd HH:mm:ss";
            break;
        case BPDateFormatterStyle_18:
            fmt = @"yyyy/M/d H:mm";
            break;
        case BPDateFormatterStyle_19:
            fmt = @"yyyy/MM/dd";
            break;
        case BPDateFormatterStyle_20:
            fmt = @"yyyy";
            break;
        case BPDateFormatterStyle_21:
            fmt = @"MM/dd";
            break;
        default:
            break;
    }
    
    NSDateFormatter *fmtter = [_fmtters objectForKey:fmt];
    if (fmtter == nil) {
        fmtter = [[NSDateFormatter alloc] init];
        fmtter.dateFormat = fmt;
        fmtter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
        [_fmtters setObject:fmtter forKey:fmt];
    }
    return fmtter;
}

+ (NSDate *)dateFromString:(NSString *)dateString fmtStyle:(BPDateFormatterStyle)style
{
    NSDateFormatter *fmtter = [[self sharedFormatter] fmtterForStyle:style];
    return [fmtter dateFromString:dateString];
}

+ (NSString *)stringFromDate:(NSDate *)date fmtStyle:(BPDateFormatterStyle)style
{
    NSDateFormatter *fmtter = [[self sharedFormatter] fmtterForStyle:style];
    return [fmtter stringFromDate:date];
}

+ (NSString *)getCurrentTimeWithFmtStyle:(BPDateFormatterStyle)style
{
    NSDateFormatter *fmtter = [[self sharedFormatter] fmtterForStyle:style];
    NSDate *datenow = [NSDate date];
    return [fmtter stringFromDate:datenow];
}

@end
